//
//  Product_Checkout2View.m
//  iFlower
//
//  Created by khanh nguyen on 7/26/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Product_Checkout2View.h"
#import "Product_Checkout3View.h"
#import "Basket.h"
#import "Request.h"
#import "iFlowerAppDelegate.h"
#import "MyAccountView.h"
#import "LoginView.h"
#import "Utility.h"
#import "Payment.h"
#import "DeliveryOptions.h"
#import "Order.h"

@implementation Product_Checkout2View
@synthesize checked;
@synthesize checked2;
@synthesize radio1;
@synthesize radio2;

@synthesize picker;
@synthesize theday;
@synthesize themonth;
@synthesize theyear;
@synthesize btnRegister;
@synthesize deceasedName = deceasedName_;
@synthesize termsView = termsView_;
@synthesize termsTextView;
@synthesize funeralTime;
-(IBAction)termsPressed:(id)sender
{
	[self.view addSubview:termsView_];
	termsView_.center = self.view.center;
}
-(IBAction)closeTermsPressed:(id)sender
{
	[termsView_ removeFromSuperview];
}
-(IBAction)buttonBack:(id)sender
{
	[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction) buttonMyAccount
{
	if ([[iFlowerAppDelegate shareDelegate] logined]) {
		MyAccountView * account = [[MyAccountView alloc] initWithNibName:@"MyAccountView" bundle:nil];
		[self.navigationController pushViewController:account animated:YES];
		[account release];
	}
	else {
		LoginView * login = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
		[self.navigationController pushViewController:login animated:YES];
		[login release];		
	}
	
}
-(IBAction)button_check1:(id)sender
{
	count1++;
	if(count1%2==1)
	{
		checked.image = [UIImage imageNamed:@"checked.png"];
		[[[iFlowerAppDelegate shareDelegate] deliveryOptions] setIs_funeral:@"Y"];
	}
	else
	{
		checked.image = NULL;
		[[[iFlowerAppDelegate shareDelegate] deliveryOptions] setIs_funeral:@"N"];
	}
}
-(IBAction)button_check2:(id)sender
{
	count2++;
	if(count2%2==1)
	{
		checked2.image = [UIImage imageNamed:@"checked.png"];
	}
	else
	{
		checked2.image = NULL;
	}
}

-(IBAction)button_radio1:(id)sender
{
	radio1.imageView.hidden = NO;
	radio2.imageView.hidden = YES;
}
-(IBAction)button_radio2:(id)sender
{
	radio2.imageView.hidden = NO;
	radio1.imageView.hidden = YES;
}

-(IBAction)button_Next:(id)sender
{
	if(count1%2>0)
	{
		if([self.funeralTime.text length]==0)
		{
			[Utility showAlert:@"Please enter Funeral time"];
			[self.funeralTime becomeFirstResponder];
			return;
		}
		if([self.deceasedName.text length]==0)
		{
			[Utility showAlert:@"Please enter Deceased Name"];
			[self.deceasedName becomeFirstResponder];
			return;			
		}
	}
	
	if(count2%2==0){
		[Utility showAlert:@"You need to agree with Term&Conditions in our website"];
		return;
	}
	[[[iFlowerAppDelegate shareDelegate] deliveryOptions] setDeceased_name:self.deceasedName.text];
	
	NSString* time = [self.funeralTime.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
	if([time length]>=4){
		NSRange range = [time rangeOfString:@":"];
		int h = [[time substringToIndex:range.location] intValue];
		NSLog(@"%d",h);
		if([[time substringFromIndex:[time length]-2] isEqualToString:@"pm"])
		{
			h+=12;
		}
		NSString* minute = [time substringFromIndex:range.location+1];
		if([minute length]>=3){
			minute = [minute substringToIndex:[minute length]-2];
		}
		int m = [minute intValue];
		[[[iFlowerAppDelegate shareDelegate] deliveryOptions] setFuneralMin:[NSString stringWithFormat:@"%d",m]];
		[[[iFlowerAppDelegate shareDelegate] deliveryOptions] setFuneralHour:[NSString stringWithFormat:@"%d",h]];
	}
	NSString* date = [NSString stringWithFormat:@"%@-%@-%@",[themonth objectAtIndex:[picker selectedRowInComponent:0]],[theday objectAtIndex:[picker selectedRowInComponent:1]],[theyear objectAtIndex:[picker selectedRowInComponent:2]]];
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	//[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault];
	[dateFormatter setDateFormat:@"MMM-d-yyyy"];
	
	NSDate *dateFromString = [[[NSDate alloc] init] autorelease];
	dateFromString = [dateFormatter dateFromString:date];
	NSDate* now = [NSDate date];
	if([now compare:dateFromString]>=0)
	{
		[Utility showAlert:@"Delivery date must equal or greater than today"];
		return;
	}
	if(radio1.imageView.hidden && radio2.imageView.hidden)
	{
		[Utility showAlert:@"Please select a payment method."];
		return;		
	}
	[[[iFlowerAppDelegate shareDelegate] order] setDeliveryDate:[NSString stringWithFormat:@"%d/%@/%@",[picker selectedRowInComponent:0]+1,[theday objectAtIndex:[picker selectedRowInComponent:1]],[theyear objectAtIndex:[picker selectedRowInComponent:2]]]];
	if(!radio1.imageView.hidden){
		[[[iFlowerAppDelegate shareDelegate] payment] setPaymentMethod:@"barclays ePDQ"];
		[Utility showAlert:@"Barclays ePDQ will be available in next version.\n Please choice Paypal for now"];
		return;		

	}else {
		[[[iFlowerAppDelegate shareDelegate] payment] setPaymentMethod:@"Paypal"];
	}

	Product_Checkout3View * check3 = [[Product_Checkout3View alloc]initWithNibName:@"Product_Checkout3View" bundle:nil];
	[self.navigationController pushViewController:check3 animated:YES];
	[check3 release];
}
-(IBAction)buttonMenu:(id)sender
{
	[self.navigationController popToRootViewControllerAnimated:YES];
}
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/
#pragma mark -
#pragma mark View lifecycle
- (void) viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	if([[iFlowerAppDelegate shareDelegate] logined])
	{
		self.btnRegister.enabled = NO;
	}else {
		self.btnRegister.enabled = YES;
	}
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	count1 = 0;
	count2 = 0;
	radio1.imageView.hidden = YES;
	radio2.imageView.hidden = YES;
	
	picker = [[UIPickerView alloc] initWithFrame:CGRectZero];
	CGSize pickerSize = [picker sizeThatFits:CGSizeZero];
	picker.frame = CGRectMake(40,190,240,pickerSize.height - 50);
	picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
	picker.delegate = self;
	picker.dataSource = self;
	picker.showsSelectionIndicator = YES;
	self.termsTextView.font = [UIFont systemFontOfSize:12.0];
	[self.view addSubview:picker];
	
	//------------------
	NSArray *firstArray = [[NSArray alloc] initWithObjects:
	@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",nil];
	self.theday = firstArray;
	[firstArray release];
	
	NSArray *secondArray = [[NSArray alloc] initWithObjects:
	@"January",@"February",@"March",@"April",@"May",@"June",@"July",@"August",@"September",@"October",@"November",@"December",nil];
	self.themonth = secondArray;
	[secondArray release];
	
	NSArray *thirdArray = [[NSArray alloc] initWithObjects:
	@"2002",@"2003",@"2004",@"2005",@"2006",@"2007",@"2008",@"2009",@"2010", @"2011", @"2012", @"2013", @"2014", @"2015", @"2016", @"2017", @"2018", @"2019", @"2020",nil];
	self.theyear = thirdArray;
	[thirdArray release];
	
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 3;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
{
	UILabel *pickerLabel = (UILabel *)view;
	if ((pickerLabel == nil) || ([pickerLabel class] != [UILabel class])) 
	{ 
		CGRect frame = CGRectMake(0.0, 0.0, 100, 32);
		pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
		pickerLabel.textAlignment = UITextAlignmentCenter;
		pickerLabel.backgroundColor = [UIColor clearColor];
		//pickerLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:15];
		pickerLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];

	}			
	pickerLabel.textColor = [UIColor blackColor];
	NSString*  text = nil;
	if (component == 0) 
	{
		text = [self.themonth objectAtIndex:row];
	}
	else
		if (component == 1) 
		{
			text =  [self.theday objectAtIndex:row];
		}
		else 
		{
			text =  [self.theyear objectAtIndex:row];
		}
	
	pickerLabel.text = text;
	
	return pickerLabel;	
	
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	if (component == 0) 
	{
		return [self.themonth count];
	}
	
	if (component == 1) 
	{
		return [self.theday count];
	}
	
	else 
	{
		return [self.theyear count];
	}
}

-(NSString *)pickerView:(UIPickerView *)pickerView
			titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{
	if (component == 0) 
	{
		return [self.themonth objectAtIndex:row];
	}
	
	if (component == 1) 
	{
		return [self.theday objectAtIndex:row];
	}
	
	else 
	{
		return [self.theyear objectAtIndex:row];
	}
}
-(CGFloat)pickerView:(UIPickerView *)pickerView
   widthForComponent:(NSInteger)component {
	if (component == 0)
		return 110;
	else if (component == 1)
		return 45;
	return 55;
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	self.btnRegister = nil;
	termsView_ = nil;
}


- (void)dealloc {
	[self.funeralTime release];
	[termsView_ release];
	[self.deceasedName release];
	[btnRegister release];
	[theday release];
	[themonth release];
	[theyear release];
    [super dealloc];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	if ([self.funeralTime isFirstResponder]) {
		[self.funeralTime resignFirstResponder];
	}
	if ([self.deceasedName isFirstResponder]) {
		[self.deceasedName resignFirstResponder];
	}
	[super touchesBegan:touches withEvent:event];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[textField resignFirstResponder];
	return YES;
}
@end
